namespace System.Xml.Serialization
{
    using System.IO;

    [Serializable]
    public class XmlBinarySerializer<T> : XmlBinaryStreamSerializer<T>, IXmlTypeSerializer<T, byte[]>
    {
        protected virtual T Deserialize(byte[] data, IXmlTypeFormatter formatter)
        {
            lock (((XmlBinarySerializer<T>)this))
            {
                if (data != null)
                {
                    try
                    {
                        using (MemoryStream ms = new MemoryStream(data))
                        {
                            return InternalStreamDeserialize(ms, formatter);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new XmlSerializationException();
                        //PluginLog.WriteErrorSync(ex);
                    }
                }
                return default(T);
            }
        }

        protected virtual byte[] Serialize(T obj, IXmlTypeFormatter formatter)
        {
            lock (((XmlBinarySerializer<T>)this))
            {
                if (!object.Equals(obj, default(T)))
                {
                    try
                    {
                        using (MemoryStream ms = InternalStreamSerialize(obj, formatter))
                        {
                            return ms.ToArray();
                        }

                    }
                    catch (Exception ex)
                    {
                        throw new XmlSerializationException();
                        //PluginLog.WriteErrorSync(ex);
                    }
                }
                return null;
            }
        }

        T IXmlTypeSerializer<T, byte[]>.Deserialize(byte[] data)
        {
            return this.Deserialize(data, XmlTypeFormatter);
        }

        T IXmlTypeSerializer<T, byte[]>.Deserialize(byte[] data, IXmlRuntimeFormatter formatter)
        {
            try
            {
                if (formatter != null)
                {
                    return this.Deserialize(data, formatter);
                }
                return this.Deserialize(data, XmlTypeFormatter);
            }
            catch (Exception ex)
            {
                throw new XmlSerializationException();
                //PluginLog.WriteErrorSync(ex);
            }
        }

        byte[] IXmlTypeSerializer<T, byte[]>.Serialize(T obj)
        {
            return this.Serialize(obj, XmlTypeFormatter);
        }

        byte[] IXmlTypeSerializer<T, byte[]>.Serialize(T obj, IXmlRuntimeFormatter formatter)
        {
            try
            {
                if (formatter != null)
                {
                    return this.Serialize(obj, formatter);
                }
                return this.Serialize(obj, XmlTypeFormatter);
            }
            catch (Exception ex)
            {
                throw new XmlSerializationException();
                //PluginLog.WriteErrorSync(ex);
            }
        }
    }
}


